const { ipcMain } = require('electron');
// const IP2Region = require('ip2region').default;


// const query = new IP2Region();
// const res = query.search('120.24.78.68');
// console.log(res);

// const getRegion = () => {
//   ipcMain.handle('on-region-server', (e, ip) => {
//     return new Promise((resolve, reject) => {
//       const query = new IP2Region({
//         ipv6db: "/tmp/db6.db",
//         disableIpv6: true
//       }
//       );
//       const res = query.search(ip || '120.24.78.68');

//       console.log(res);
//       resolve(res);
//     });
//   });
// };

const axios = require('axios');
const iconv = require('iconv-lite');

const getRegion = () => {
  ipcMain.handle('on-region-server', (e, ip) => {
    return new Promise((resolve, reject) => {
      axios.get(`https://whois.pconline.com.cn/ipJson.jsp?ip=${ip}&json=true`,{responseType: 'arraybuffer'}).then(res => {
        const content = iconv.decode(res.data, 'GBK');
        resolve(content);
      }).catch((error) => {
        console.log(error)
      })
    });
  });
};


module.exports = getRegion;
